package cn.lvxs.framework;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wabacus.util.RegexTools;

/**
 * Servlet Filter implementation class SessionFilter
 */
public class SessionFilter implements Filter {
	//登录页面地址
	private String LOGINFORMURL="/login.html";
	private String NeedLoginURL = "(/(\\S*?))|(/ShowReport\\.wx)";
    /**
     * Default constructor. 
     */
    public SessionFilter() {
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		String currentURL = request.getRequestURI(); // 获得根目录对应的绝对路径
		String contextPath = request.getContextPath(); // 获得应用程序路径
		currentURL=currentURL.replaceFirst(contextPath, "");
		if(currentURL.equalsIgnoreCase("/")){
			response.sendRedirect("index.html");
			response.getWriter().close();
			return;
		}
		if (!currentURL.endsWith("/")
				&& currentURL.lastIndexOf("/") >= 0
				&& currentURL.substring(currentURL.lastIndexOf("/") + 1)
						.indexOf(".") < 0) {
			response.sendRedirect(currentURL+"/");
			response.getWriter().close();
			return;
		}
		if (currentURL.endsWith(".jsp")) {
			response.sendRedirect(currentURL.substring(0,
					currentURL.length() - 4) + ".html");
			response.getWriter().close();
			return;
		}
		if (RegexTools.isMatch(currentURL, NeedLoginURL)) {
			if (currentURL.endsWith(".html") || currentURL.endsWith(".wx")
					|| currentURL.endsWith("/")) {
				if (!currentURL.equalsIgnoreCase(LOGINFORMURL)) {
					HttpSession session = (HttpSession) request.getSession();
					if (session == null
							|| session.getAttribute("UserID") == null) {
						response.setCharacterEncoding("UTF-8");
						response.setContentType("text/html;charset=UTF-8");
						response.getWriter()
								.println(
										"<html><script>var _hmt = _hmt || [];(function() {var hm = document.createElement(\"script\");hm.src = \"//hm.baidu.com/hm.js?438385a1f9cff94bf447d684aa50d51b\";var s = document.getElementsByTagName(\"script\")[0];s.parentNode.insertBefore(hm, s);})();</script>"
												+"<script type=\"text/javascript\">window.top.location=\""
												+ request.getContextPath()
												+ LOGINFORMURL
												+ "\";</script></html>");
						response.getWriter().close();
						return;
					} else {
						long UserID = 0;
						try {
							UserID = (Long) session.getAttribute("UserID");
						} catch (Exception _err) {
						}
						if (UserID < 1) {
							response.setCharacterEncoding("UTF-8");
							response.setContentType("text/html;charset=UTF-8");
							response.getWriter()
									.println("<html><script>var _hmt = _hmt || [];(function() {var hm = document.createElement(\"script\");hm.src = \"//hm.baidu.com/hm.js?438385a1f9cff94bf447d684aa50d51b\";var s = document.getElementsByTagName(\"script\")[0];s.parentNode.insertBefore(hm, s);})();</script>"
											+"<script type=\"text/javascript\">alert(\"您没有登录或者已经超时退出，请重新登录!\");window.top.location=\""
													+ request.getContextPath()
													+ LOGINFORMURL
													+ "\";</script></html>");
							response.getWriter().close();
							return;
						}
					}
				}
			}
		}
		// pass the request along the filter chain
		chain.doFilter(servletRequest, servletResponse);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
	}

}
